<template>
	<view class="container">
		<!-- 表单区域 -->
		<view class="form-content">
			<!-- 总胆固醇 -->
			<view class="form-item">
				<view class="label">
					<text class="required">*</text>
					<text class="text-black">总胆固醇</text>
				</view>
				<view class="input-wrapper" @tap="showTotalCholesterolPicker">
					<text
						:class="totalCholesterol !== '' ? 'selected-value' : 'placeholder'">{{ totalCholesterol !== '' ? totalCholesterol + ' mmol/L' : '请选择总胆固醇' }}</text>
					<text class="arrow">></text>
				</view>
			</view>

			<!-- 甘油三酯 -->
			<view class="form-item">
				<view class="label">
					<text class="required">*</text>
					<text class="text-black">甘油三酯</text>
				</view>
				<view class="input-wrapper" @tap="showTriglyceridesPicker">
					<text
						:class="triglycerides !== '' ? 'selected-value' : 'placeholder'">{{ triglycerides !== '' ? triglycerides + ' mmol/L' : '请选择甘油三酯' }}</text>
					<text class="arrow">></text>
				</view>
			</view>

			<!-- 高密度脂蛋白 -->
			<view class="form-item">
				<view class="label">
					<text class="required">*</text>
					<text class="text-black">高密度脂蛋白</text>
				</view>
				<view class="input-wrapper" @tap="showHDLPicker">
					<text
						:class="hdl !== '' ? 'selected-value' : 'placeholder'">{{ hdl !== '' ? hdl + ' mmol/L' : '请选择高密度脂蛋白' }}</text>
					<text class="arrow">></text>
				</view>
			</view>

			<!-- 低密度脂蛋白 -->
			<view class="form-item">
				<view class="label">
					<text class="required">*</text>
					<text class="text-black">低密度脂蛋白</text>
				</view>
				<view class="input-wrapper" @tap="showLDLPicker">
					<text
						:class="ldl !== '' ? 'selected-value' : 'placeholder'">{{ ldl !== '' ? ldl + ' mmol/L' : '请选择低密度脂蛋白' }}</text>
					<text class="arrow">></text>
				</view>
			</view>

			<!-- 总胆固醇选择器 -->
			<uni-popup ref="totalCholesterolPopup" type="bottom">
				<view class="picker-container">
					<view class="picker-header">
						<text @tap="hideTotalCholesterol" style="color: #c8cacf;">取消</text>
						<view class="title">
							<text class="selected-value text-blod" style="font-weight: 600;font-size: 34rpx;">总胆固醇
								{{ tempTotalCholesterol }} mmol/L</text>
						</view>
						<text @tap="confirmTotalCholesterol" style="color: #4cd080;">确定</text>
					</view>
					<view class="picker-content">
						<picker-view :value="[totalCholesterolRange.indexOf(tempTotalCholesterol)]"
							@change="onTotalCholesterolChange" class="picker-view">
							<picker-view-column>
								<view class="picker-item" v-for="item in totalCholesterolRange" :key="item">{{ item }}
								</view>
							</picker-view-column>
						</picker-view>
					</view>
				</view>
			</uni-popup>

			<!-- 甘油三酯选择器 -->
			<uni-popup ref="triglyceridesPopup" type="bottom">
				<view class="picker-container">
					<view class="picker-header">
						<text @tap="hideTriglycerides" style="color: #c8cacf;">取消</text>
						<view class="title">
							<text class="selected-value text-blod" style="font-weight: 600;font-size: 34rpx;">甘油三酯
								{{ tempTriglycerides }} mmol/L</text>
						</view>
						<text @tap="confirmTriglycerides" style="color: #4cd080;">确定</text>
					</view>
					<view class="picker-content">
						<picker-view :value="[triglyceridesRange.indexOf(tempTriglycerides)]"
							@change="onTriglyceridesChange" class="picker-view">
							<picker-view-column>
								<view class="picker-item" v-for="item in triglyceridesRange" :key="item">{{ item }}
								</view>
							</picker-view-column>
						</picker-view>
					</view>
				</view>
			</uni-popup>

			<!-- 高密度脂蛋白选择器 -->
			<uni-popup ref="hdlPopup" type="bottom">
				<view class="picker-container">
					<view class="picker-header">
						<text @tap="hideHDL" style="color: #c8cacf;">取消</text>
						<view class="title">
							<text class="selected-value text-blod" style="font-weight: 600;font-size: 34rpx;">高密度脂蛋白
								{{ tempHDL }} mmol/L</text>
						</view>
						<text @tap="confirmHDL" style="color: #4cd080;">确定</text>
					</view>
					<view class="picker-content">
						<picker-view :value="[hdlRange.indexOf(tempHDL)]" @change="onHDLChange" class="picker-view">
							<picker-view-column>
								<view class="picker-item" v-for="item in hdlRange" :key="item">{{ item }}</view>
							</picker-view-column>
						</picker-view>
					</view>
				</view>
			</uni-popup>

			<!-- 低密度脂蛋白选择器 -->
			<uni-popup ref="ldlPopup" type="bottom">
				<view class="picker-container">
					<view class="picker-header">
						<text @tap="hideLDL" style="color: #c8cacf;">取消</text>
						<view class="title">
							<text class="selected-value text-blod" style="font-weight: 600;font-size: 34rpx;">低密度脂蛋白
								{{ tempLDL }} mmol/L</text>
						</view>
						<text @tap="confirmLDL" style="color: #4cd080;">确定</text>
					</view>
					<view class="picker-content">
						<picker-view :value="[ldlRange.indexOf(tempLDL)]" @change="onLDLChange" class="picker-view">
							<picker-view-column>
								<view class="picker-item" v-for="item in ldlRange" :key="item">{{ item }}</view>
							</picker-view-column>
						</picker-view>
					</view>
				</view>
			</uni-popup>

			<!-- 测量时间 -->
			<view class="form-item">
				<view class="label">
					<text class="required">*</text>
					<text class="text-black">测量时间</text>
				</view>
				<view class="input-wrapper" @tap="showTimePicker">
					<text
						:class="selectedTime ? 'selected-value' : 'placeholder'">{{ selectedTime || '请选择测量时间' }}</text>
					<text class="arrow">></text>
				</view>
			</view>

			<!-- 时间选择器 -->
			<uni-popup ref="timePopup" type="bottom">
				<view class="picker-container">
					<view class="picker-header">
						<text @tap="hideTimePicker" style="color: #c8cacf;">取消</text>
						<view class="title">
							<text class="selected-value text-blod"
								style="font-weight: 600;font-size: 34rpx;">{{ formatTempTime }}</text>
						</view>
						<text @tap="confirmTimePicker" style="color: #4cd080;">确定</text>
					</view>
					<view class="picker-content">
						<picker-view :value="[yearIndex, monthIndex, dayIndex, hourIndex, minuteIndex, secondIndex]"
							@change="onTimeChange" class="picker-view">
							<picker-view-column>
								<view class="picker-item" v-for="year in years" :key="year">{{ year }}年</view>
							</picker-view-column>
							<picker-view-column>
								<view class="picker-item" v-for="month in months" :key="month">{{ month }}月</view>
							</picker-view-column>
							<picker-view-column>
								<view class="picker-item" v-for="day in days" :key="day">{{ day }}日</view>
							</picker-view-column>
							<picker-view-column>
								<view class="picker-item" v-for="hour in hours" :key="hour">
									{{ hour.toString().padStart(2, '0') }}时</view>
							</picker-view-column>
							<picker-view-column>
								<view class="picker-item" v-for="minute in minutes" :key="minute">
									{{ minute.toString().padStart(2, '0') }}分</view>
							</picker-view-column>
							<picker-view-column>
								<view class="picker-item" v-for="second in seconds" :key="second">
									{{ second.toString().padStart(2, '0') }}秒</view>
							</picker-view-column>
						</picker-view>
					</view>
				</view>
			</uni-popup>
		</view>

		<!-- 保存按钮 -->
		<view class="save-button" @tap="saveData">保存</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from "vuex"
	import {
		uniPopup
	} from '@dcloudio/uni-ui'
	import {
		addBloodLipid
	} from '@/api/healthData/everyBloodLipid.js'

	export default {
		computed: mapGetters(['name', 'id']),
		components: {
			uniPopup
		},
		data() {
			return {
				measurementTypes: [],
				selectedType: 'empty',
				totalCholesterol: '',
				triglycerides: '',
				hdl: '',
				ldl: '',
				tempTotalCholesterol: 4.9,
				tempTriglycerides: 2.1,
				tempHDL: 5.6,
				tempLDL: 10.0,
				totalCholesterolRange: [],
				triglyceridesRange: [],
				hdlRange: [],
				ldlRange: [],
				selectedTime: '',
				tempTime: {
					year: new Date().getFullYear(),
					month: new Date().getMonth() + 1,
					day: new Date().getDate(),
					hour: new Date().getHours(),
					minute: new Date().getMinutes(),
					second: new Date().getSeconds()
				},
				years: [],
				months: Array.from({
					length: 12
				}, (_, i) => i + 1),
				days: Array.from({
					length: 31
				}, (_, i) => i + 1),
				hours: Array.from({
					length: 24
				}, (_, i) => i),
				minutes: Array.from({
					length: 60
				}, (_, i) => i),
				seconds: Array.from({
					length: 60
				}, (_, i) => i)
			}
		},
		onLoad(options) {
			this.targetId = options.targetId
			this.queryTypeList()
		},
		computed: {
			yearIndex() {
				return this.years.indexOf(this.tempTime.year)
			},
			monthIndex() {
				return this.months.indexOf(this.tempTime.month)
			},
			dayIndex() {
				return this.days.indexOf(this.tempTime.day)
			},
			hourIndex() {
				return this.hours.indexOf(this.tempTime.hour)
			},
			minuteIndex() {
				return this.minutes.indexOf(this.tempTime.minute)
			},
			secondIndex() {
				return this.seconds.indexOf(this.tempTime.second)
			},
			formatTempTime() {
				const {
					year,
					month,
					day,
					hour,
					minute,
					second
				} = this.tempTime
				const period = hour < 12 ? '上午' : '下午'
				const displayHour = hour % 12 || 12
				return `${period} ${displayHour}:${minute.toString().padStart(2, '0')}:${second.toString().padStart(2, '0')}`
			},
			formatSaveTime() {
				const {
					year,
					month,
					day,
					hour,
					minute,
					second
				} = this.tempTime
				return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}:${second.toString().padStart(2, '0')}`
			}
		},
		created() {
			// 年份范围
			const currentYear = new Date().getFullYear()
			for (let i = currentYear - 5; i <= currentYear + 5; i++) {
				this.years.push(i)
			}

			// 血脂正常范围值生成
			// 总胆固醇: 2.8-5.2 mmol/L
			for (let i = 0.1; i <= 20.0; i += 0.1) {
				this.totalCholesterolRange.push(parseFloat(i.toFixed(1)))
			}

			// 甘油三酯: 0.4-1.7 mmol/L
			for (let i = 0.1; i <= 30.0; i += 0.1) {
				this.triglyceridesRange.push(parseFloat(i.toFixed(1)))
			}

			// 高密度脂蛋白: 1.0-2.0 mmol/L
			for (let i = 0.1; i <= 5.0; i += 0.1) {
				this.hdlRange.push(parseFloat(i.toFixed(1)))
			}

			// 低密度脂蛋白: 2.1-3.1 mmol/L
			for (let i = 0.1; i <= 15.0; i += 0.1) {
				this.ldlRange.push(parseFloat(i.toFixed(1)))
			}
		},
		methods: {
			// 总胆固醇选择器
			showTotalCholesterolPicker() {
				this.$refs.totalCholesterolPopup.open()
			},
			hideTotalCholesterol() {
				this.$refs.totalCholesterolPopup.close()
			},
			onTotalCholesterolChange(e) {
				const [idx] = e.detail.value;
				this.tempTotalCholesterol = this.totalCholesterolRange[idx]
			},
			confirmTotalCholesterol() {
				this.totalCholesterol = this.tempTotalCholesterol;
				this.hideTotalCholesterol()
			},

			// 甘油三酯选择器
			showTriglyceridesPicker() {
				this.$refs.triglyceridesPopup.open()
			},
			hideTriglycerides() {
				this.$refs.triglyceridesPopup.close()
			},
			onTriglyceridesChange(e) {
				const [idx] = e.detail.value;
				this.tempTriglycerides = this.triglyceridesRange[idx]
			},
			confirmTriglycerides() {
				this.triglycerides = this.tempTriglycerides;
				this.hideTriglycerides()
			},

			// 高密度脂蛋白选择器
			showHDLPicker() {
				this.$refs.hdlPopup.open()
			},
			hideHDL() {
				this.$refs.hdlPopup.close()
			},
			onHDLChange(e) {
				const [idx] = e.detail.value;
				this.tempHDL = this.hdlRange[idx]
			},
			confirmHDL() {
				this.hdl = this.tempHDL;
				this.hideHDL()
			},

			// 低密度脂蛋白选择器
			showLDLPicker() {
				this.$refs.ldlPopup.open()
			},
			hideLDL() {
				this.$refs.ldlPopup.close()
			},
			onLDLChange(e) {
				const [idx] = e.detail.value;
				this.tempLDL = this.ldlRange[idx]
			},
			confirmLDL() {
				this.ldl = this.tempLDL;
				this.hideLDL()
			},

			selectType(type) {
				this.selectedType = type
			},
			showTimePicker() {
				if (!this.selectedTime) {
					const now = new Date()
					this.tempTime = {
						year: now.getFullYear(),
						month: now.getMonth() + 1,
						day: now.getDate(),
						hour: now.getHours(),
						minute: now.getMinutes(),
						second: now.getSeconds()
					}
				}
				this.$refs.timePopup.open()
			},
			hideTimePicker() {
				this.$refs.timePopup.close()
			},
			onTimeChange(e) {
				const [y, m, d, h, mi, s] = e.detail.value
				this.tempTime = {
					year: this.years[y],
					month: this.months[m],
					day: this.days[d],
					hour: this.hours[h],
					minute: this.minutes[mi],
					second: this.seconds[s]
				}
			},
			confirmTimePicker() {
				this.selectedTime = this.formatTempTime;
				this.hideTimePicker()
			},
			saveData() {
				if (this.totalCholesterol === '' || this.triglycerides === '' || this.hdl === '' || this.ldl === '') {
					uni.showToast({
						title: '请选择所有血脂值',
						icon: 'none'
					});
					return;
				}
				if (!this.selectedTime) {
					uni.showToast({
						title: '请选择测量时间',
						icon: 'none'
					});
					return;
				}

				const data = {}
				data.measuredTime = this.formatSaveTime
				data.tc = this.totalCholesterol
				data.tg = this.triglycerides
				data.hdl = this.hdl
				data.ldl = this.ldl
				data.sourceType = '1'
				data.sourceName = '手工录入'
				// data.measuredType = this.selectedType
				data.userId = this.id

				addBloodLipid(data).then(res => {
					uni.navigateTo({
						url: `/subPackage_1/pages/index/healthData/blood-lipid-success?totalCholesterol=${encodeURIComponent(this.totalCholesterol)}&triglycerides=${encodeURIComponent(this.triglycerides)}&hdl=${encodeURIComponent(this.hdl)}&ldl=${encodeURIComponent(this.ldl)}&measuredTime=${encodeURIComponent(this.selectedTime)}&id=${res.data}`
					})
				})
			},
			queryTypeList() {
				// queryBloodLipidTypeList({
				// 	targetId: this.targetId
				// }).then(res => {
				// 	this.measurementTypes = res.data || []
				// })
			}
		}
	}
</script>

<style>
	.container {
		min-height: 100vh;
		background-color: #fff;
		padding-top: 20px;
	}

	.form-content {
		background-color: #fff;
		padding: 0 30rpx;
	}

	.form-item {
		padding: 30rpx 0;
		display: flex;
		align-items: center;
		justify-content: space-between;
		background-color: #fbfbfd;
		margin-bottom: 20rpx;
		border-radius: 10rpx;
	}

	.label {
		display: flex;
		align-items: center;
		gap: 10rpx;
		padding-left: 20rpx;
	}

	.required {
		color: #ff4d4f;
		margin-right: 6rpx;
	}

	.placeholder {
		color: #c8cacf;
	}

	.selected-value {
		color: #333;
		font-weight: 600;
	}

	.input-wrapper {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		padding: 0 20rpx;
	}

	.arrow {
		color: #c8cacf;
	}

	.type-options {
		display: flex;
		flex-wrap: wrap;
		gap: 16rpx;
		padding: 0 20rpx 10rpx 20rpx;
	}

	.type-item {
		background-color: #fff;
		color: #666;
		padding: 14rpx 22rpx;
		border-radius: 100rpx;
		border: 1rpx solid #eee;
	}

	.type-item.selected {
		color: #4cd080;
		border-color: #4cd080;
		background: #eafff6;
	}

	.picker-container {
		background: #fff;
		border-top-left-radius: 16rpx;
		border-top-right-radius: 16rpx;
	}

	.picker-header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 22rpx 30rpx;
	}

	.picker-content {
		height: 460rpx;
	}

	.picker-view {
		width: 100%;
		height: 100%;
	}

	.picker-item {
		height: 80rpx;
		line-height: 80rpx;
		text-align: center;
	}

	.save-button {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 40rpx;
		height: 90rpx;
		background-color: #4cd080;
		color: #fff;
		border-radius: 100rpx;
		text-align: center;
		line-height: 90rpx;
		font-weight: 600;
	}
</style>